//
//  LZPopSelectView.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/6.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZPopSelectView.h"
#import "LZPopSelectCollectViewCell.h"
#import "LZPopSelectCollectViewModel.h"

@interface LZPopSelectView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *allBgView;
@property (weak, nonatomic) IBOutlet UILabel *tileLabel;
@property (weak, nonatomic) IBOutlet UIButton *bottomBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectView;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSMutableArray *resultArr;
@end

@implementation LZPopSelectView

- (void)awakeFromNib {
    [super awakeFromNib];
    
     [self configureView];
}

#pragma mark -
#pragma mark UI
- (void)configureView
{
    self.backgroundColor = UIColorFromRGB(0x111111, 0.7);
    self.allBgView.layer.cornerRadius = 8;
    self.allBgView.layer.masksToBounds = YES;
    self.allBgView.alpha = 1;
    self.resultArr = @[].mutableCopy;
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    [self.collectView registerNib:[UINib nibWithNibName:@"LZPopSelectCollectViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.bottomBtn setTitle:LZLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
}

#pragma mark -
#pragma mark private
- (IBAction)bottomBtnAction:(id)sender {
    if (self.selectedItemsBlock) {
        self.selectedItemsBlock(self.resultArr.copy);
    }
    [self hiddenSelfAnimation];
}
- (IBAction)cancelBtnAction:(id)sender {
    
    [self hiddenSelfAnimation];
}

/**
 获取文字宽度

 @param str 内容
 @return <#return value description#>
 */
- (CGFloat)getTheTextWidth:(NSString *)str
{
    CGFloat w = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.width;
    
    return w + 40;
}


/**
 返回相应的cell应该展示的宽度

 @param str 内容
 @return <#return value description#>
 */
- (CGFloat)getCanShowTextWidth:(NSString *)str
{
    CGFloat strW = [self getTheTextWidth:str];
    CGFloat canShowW = SCREEN_WIDTH - 70;
    return  strW > canShowW ? canShowW : strW;
}


/**
 处理允许多选的数据源

 @param m <#m description#>
 */
- (void)changeModelStateForMoreWithModel:(LZPopSelectCollectViewModel *)m
{
    for (int i = 0; i < self.dataArray.count; i ++) {
        LZPopSelectCollectViewModel *mTemp = self.dataArray[i];
        if ([mTemp.contentName isEqualToString:m.contentName]) {
            mTemp.isSelected = !m.isSelected;
            if (mTemp.isSelected) {
                [self.resultArr addObject:mTemp];
            }else{
                [self.resultArr removeObject:mTemp];
            }
        }
    }
    
}


/**
 处理只能单选的数据源

 @param m <#m description#>
 */
- (void)changeModelStateForSingelWithModel:(LZPopSelectCollectViewModel *)m
{
    for (int i = 0; i <self.dataArray.count; i ++) {
        LZPopSelectCollectViewModel *mTemp = self.dataArray[i];
        if ([mTemp.contentName isEqualToString:m.contentName]) {
            mTemp.isSelected = !m.isSelected;
            if (mTemp.isSelected) {
                [self.resultArr removeAllObjects];
                [self.resultArr addObject:mTemp];
            }else{
                [self.resultArr removeAllObjects];
            }
        }else{
            mTemp.isSelected = NO;
        }
    }
}

#pragma mark -
#pragma mark 对外方法
- (void)loadTitle:(NSString *)title
{
    self.tileLabel.text = title;
}

- (void)loadDataArr:(NSArray *)dataArr
{
    self.dataArray = dataArr;
    [self.collectView reloadData];
    if (self.resultArr.count > 0) {
        [self.resultArr removeAllObjects];
    }
    
    for (int i = 0; i < self.dataArray.count; i ++) {
        LZPopSelectCollectViewModel *m = self.dataArray[i];
        if (m.isSelected) {
            [self.resultArr addObject:m];
        }
    }
}

- (void)showSelfAnimation
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 1;
    WS(wself);
    [UIView animateWithDuration:0.3 animations:^{
        wself.allBgView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hiddenSelfAnimation
{
   [UIView animateWithDuration:0.3 animations:^{
       self.allBgView.alpha = 0;
        self.alpha = 0;
   } completion:^(BOOL finished) {
     
        [self removeFromSuperview];
   }];
}


#pragma mark -
#pragma mark collectionview delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LZPopSelectCollectViewModel *m = self.dataArray[indexPath.row];
    CGFloat w = [self getCanShowTextWidth:m.contentName];
    
    return CGSizeMake(w, 40);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LZPopSelectCollectViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LZPopSelectCollectViewModel *m = self.dataArray[indexPath.row];
    [cell loadData:m.contentName];
    [cell showStateForSelected:m.isSelected];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LZPopSelectCollectViewModel *m = self.dataArray[indexPath.row];
    if (self.canSelectMore) {
        [self changeModelStateForMoreWithModel:m];
    }else{
        [self changeModelStateForSingelWithModel:m];
    }
    [self.collectView reloadData];
}

@end
